package com.superdry.activity;

import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.ab.http.AbHttpUtil;
import com.ab.http.AbRequestParams;
import com.ab.http.AbStringHttpResponseListener;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.drysurper.R;
import com.superdry.json.Userinfo;
import com.superdry.utils.GlobalConst;
import com.superdry.utils.SPHelper;
import com.superdry.utils.StringUtils;
import com.superdry.utils.ToastHelper;

public class RegisterActivity extends Activity implements OnClickListener {

	private EditText phone_num;
	private EditText verify_code;
	private Button login;
	private Button get_verify_code;
	private ProgressDialog progressDialog;
	private LinearLayout register_goback;
	
	private AbHttpUtil mAbHttpUtil = null;
	private static int COUNT = 60;

	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			if (COUNT > 0) {
				get_verify_code.setText("(" + COUNT + "s) 获取");
			} else {
				get_verify_code.setClickable(true);
				get_verify_code.setText("重新获取");
				COUNT = 60;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		initViews();
	}

	/**
	 * 初始化组件
	 */
	private void initViews() {

		// 获取Http工具类
		mAbHttpUtil = AbHttpUtil.getInstance(this);
		mAbHttpUtil.setTimeout(10000);

		phone_num = (EditText) findViewById(R.id.phone_num);
		verify_code = (EditText) findViewById(R.id.verify_code);
		register_goback = (LinearLayout) findViewById(R.id.register_goback);
		
		login = (Button) findViewById(R.id.login);
		get_verify_code = (Button) findViewById(R.id.get_verify_code);
		
		register_goback.setOnClickListener(this);
		login.setOnClickListener(this);
		get_verify_code.setOnClickListener(this);
	}

	@Override
	public void onClick(View view) {

		switch (view.getId()) {
		case R.id.login:
			// TODO 此处调用登入接口
			login();
			// 冒充登入成功
			break;
		case R.id.get_verify_code:
			// TODO 此处调用验证码接口
			getVerifyCode();
			break;
		case R.id.register_goback:
			finish();
			break;

		default:
			break;
		}
	}
	/**
	 * 登入 
	 */
	private void login() {
		String phone = phone_num.getText().toString();
		String verifycode = verify_code.getText().toString();
		if(StringUtils.isEmpty(phone)){
			ToastHelper.show(RegisterActivity.this, "手机号不能为空");
		}
		if(StringUtils.isEmpty(verifycode)){
			ToastHelper.show(RegisterActivity.this, "请输入验证码");
		}
		//网络请求
		AbRequestParams params = new AbRequestParams();
		params.put("phoneNumber", phone);
		params.put("random", verifycode);
		mAbHttpUtil.post(GlobalConst.GET_REGISTER, params, new AbStringHttpResponseListener() {
			
			@Override
			public void onStart() {
				progressDialog = progressDialog.show(RegisterActivity.this, "注册中...", "请稍后 ...");
//				AbDialogUtil.showProgressDialog(RegisterActivity.this,0,"正在登入...");
			}
			
			@Override
			public void onFinish() {
				progressDialog.dismiss();
			}
			
			@Override
			public void onFailure(int statusCode, String content, Throwable error) {
				progressDialog.dismiss();
			}
			
			@Override
			public void onSuccess(int statusCode, String content) {
				if(content.startsWith("{")){
					//登入失败
					ToastHelper.show(RegisterActivity.this, "输入的验证码有误");
					return;
				}
				List<Userinfo> info = JSON.parseArray(content, Userinfo.class);
				//登入成功 将user信息保存到sp中
				SPHelper.putString(RegisterActivity.this, SPHelper.USERINFO, content);
				//登入成功后 跳入到 下单页面
				Intent intent = new Intent(RegisterActivity.this, ChooseClothTypeActivity.class);
				startActivity(intent);
			}
		});
		
	}

	/**
	 * 得到验证码
	 */
	private void getVerifyCode() {
		String phone = phone_num.getText().toString();
		if ("".equals(phone)) {
			ToastHelper.show(RegisterActivity.this, "发送手机号码不能为空！");
			return;
		}
		String regex = "^((13[0-9])|(15[^4,\\D])|(18[0-9])|(14[0-9])|(17[0-9]))\\d{8}$";
		if (!StringUtils.matchStr(phone, regex)) {
			ToastHelper.show(RegisterActivity.this, "请输入正确的手机号码！");
			return;
		}
		get_verify_code.setClickable(false);
		// 向服务其发送 获取验证码请求
		AbRequestParams params = new AbRequestParams();
		params.put("phoneNumber", phone);
		// 放入参数
		mAbHttpUtil.post(GlobalConst.GET_VERIFYCODE, params, new AbStringHttpResponseListener() {

			@Override
			public void onSuccess(int statusCode, String content) {
				// 开始进入倒计时
				new Thread() {
					public void run() {
						while (0 < COUNT) {
							try {
								Thread.sleep(1000);
								COUNT--;
								handler.sendEmptyMessage(0);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
					};
				}.start();
			}
			@Override
			public void onStart() {
				
			}

			@Override
			public void onFinish() {

			}

			@Override
			public void onFailure(int statusCode, String content, Throwable error) {
				ToastHelper.show(RegisterActivity.this, "获取失败，请检查网络");
			}

		});
	}
}
